package com.cjb.cloudsystem.controller;

import com.cjb.cloudsecurity.JWT.TokenStoreCache;
import com.cjb.cloudsystem.entity.SysUserToken;
import com.cjb.cloudsystem.jwt.TokenUtil;
import com.cjb.cloudsystem.service.SysAuthService;
import com.cjb.cloudsystem.vo.SysAccountLoginVO;
import com.cjb.common.security.TokenUtils;
import com.cjb.common.util.HttpContextUtils;
import com.cjb.common.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * All rights Reserved, Designed By  chenjinbin
 *
 * @author chenjinbin
 * @Project: fjbd_cjb_bs
 * @Package com.cjb.cloudsystem.controller
 * @title SysAuthController
 * @Description
 * @email
 * @date 2023-08-28 16:57
 * @since JDK 1.8
 */
@RestController
@RequestMapping(value = {"sys/auth"})
@AllArgsConstructor
public class SysAuthController {
    private final SysAuthService sysAuthService;
    /**
     * token缓冲区注入
     */
    private final TokenStoreCache tokenStoreCache;

    @PostMapping("login")
    @Operation(summary = "账号密码登录")
    public Result<SysUserToken> login(@RequestBody SysAccountLoginVO login) {
        SysUserToken token = sysAuthService.loginByAccount(login);

        return Result.success(token);
    }

    @PostMapping("logout")
    @Operation(summary = "退出登录")
    public Result<SysUserToken> logout() {

        //消除缓存token
        String accessToken = TokenUtils.getAccessToken(HttpContextUtils.getHttpServletRequest());
        tokenStoreCache.deleteUser(accessToken);
        return Result.success();
    }
}
